<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2022/12/5
 * Time: 17:21
 */

namespace app\controller\sys;
use app\model\app\AppLoanOrder AS model;
use app\model\app\AppPay;
use app\model\app\AppPayOrder;
use app\model\sys\SysAutoPayConfig;
use app\model\sys\SysUser;
use app\model\tb\TbUser;

class Order extends Base
{
    public function list(){
        $where = $this->where;
        $post = input('post.');
        !empty($post['user_id']) && $where[] = ['user_id','=',$post['user_id']];
        !empty($post['kefu_name']) && $where[] = ['kefu_name','like','%'.$post['kefu_name'].'%'];
        !empty($this->start_time) && $where[] = ['create_time','>=',$this->start_time];
        !empty($this->end_time) && $where[] = ['create_time','<=',$this->end_time];
        if (!empty($post['not_pay'])){
            $ids = AppPayOrder::where('status',0)->field('order_id')->select()->toArray();
            $ids = array_column($ids,'order_id');
            $where[] = ['id','IN',$ids];
        }
        if (!empty($post['mobile'])){
            $ids = TbUser::field('user_id')->where('mobile','lik   e','%'.$post['mobile'].'%')->select()->toArray();
            $ids = array_column($ids,'user_id');
            $where[] = ['user_id','IN',$ids];
        }
        $list = model::where($where)
            ->with(['user','basic','pay_order'])
            ->order('create_time DESC')
            ->paginate([
                'page' => $this->page,
                'list_rows' => $this->size
            ]);
        return $this->msg($list);
    }


    public function info(){
        $id = input('id');
        $user = model::where('id',$id)->with(['user','basic'])->find();
        return $this->msg($user);
    }

    public function kefu(){
        $id = input('id');
        $kefu_id = input('kefu_id');
        model::where('id','IN',$id)
            ->update([
                'kefu_id' => $kefu_id,
                'kefu_name' => SysUser::where('user_id',$kefu_id)->value('username')
            ]);
        return $this->msg();
    }

    public function supplement_order(){
        $mobile = input('mobile');
        $is_credit = input('is_credit');
        $pay_amount = input('pay_amount');
        $user_id = TbUser::where('mobile',$mobile)->value('user_id');
        if (empty($user_id))
            return $this->fail('手机号码错误未查找到用户');
        $find = model::where('user_id',$user_id)->find();
        if (!empty($is_credit)) {
            empty($find->credit_amount) && $find->credit_amount = 0;
            $find->credit_amount = bcadd($find->credit_amount, $pay_amount, 2);
            $find->save();
        }
        $config = SysAutoPayConfig::field('pay_title,pay_content,process_content,process_title')->where('status', 1)->find()->toArray();
        AppPayOrder::where('user_id',$find->user_id)->where('status',0)->save(['status' => 2]);
        $data = [
            'order_id' => $find->id,
            'user_id' => $find->user_id,
            'name' => $find->basic->branch_name,
            'kefu_id' => $find->kefu_id,
            'kefu_name' => $find->kefu->username,
            'pay_content' => $config['pay_content'],
            'pay_name' => '',
            'pay_account' => $find->pay_account,
            'pay_title' => $config['pay_title'],
            'pay_amount' => $pay_amount,
            'order_code' => 'C'.date('YmdHis').rand(10000,99999),
            'email' => $find->user->email,
            'phone' => $find->user->mobile,
            'status' => 0,
            'create_time' => date('Y-m-d H:i:s'),
            'update_time' => date('Y-m-d H:i:s'),
            'is_credit' =>  $is_credit,
            'is_sys' => 0
        ];
        $find = AppPayOrder::create($data);
        return $this->msg($find);
    }

    public function create_pay_order(){
        $id = input('id');
        $post = input('post.');
        $find = model::find($id);
//        if (!empty($post['is_credit'])) {
//            empty($find->credit_amount) && $find->credit_amount = 0;
//            $find->credit_amount = bcadd($find->credit_amount, $post['pay_amount'], 2);
//            $find->save();
//        }
        if (!empty($post['is_auto'])) {
            $config = SysAutoPayConfig::field('pay_title,pay_content,process_content,process_title')->where('status', 1)->find()->toArray();
            foreach ($config AS $key => $value) $post[$key] = $value;
        }
        AppPayOrder::where('user_id',$find->user_id)->where('status',0)->save(['status' => 2]);
        $data = [
            'order_id' => $find->id,
            'user_id' => $find->user_id,
            'name' => $find->basic->branch_name,
            'kefu_id' => $find->kefu_id,
            'kefu_name' => $find->kefu->username,
            'pay_content' => $post['pay_content'],
            'pay_title' => $post['pay_title'],
            'pay_amount' => $post['pay_amount'],
            'pay_name' => AppPay::where('pay_ds',1)->value('pay_name'),
            'pay_account' => $find->pay_account,
            'order_code' => 'C'.date('YmdHis').rand(10000,99999),
            'email' => $find->user->email,
            'phone' => $find->user->mobile,
            'status' => 0,
            'create_time' => date('Y-m-d H:i:s'),
            'update_time' => date('Y-m-d H:i:s'),
            'is_credit' => $post['is_credit'],
            'is_sys' => 0
        ];
        $find = AppPayOrder::create($data);
        return $this->msg($find);
    }

    public function save(){
        $post = input('post.');
        if (empty($post['id'])) {
            model::create($post);
        }else{
            $find = model::where('id',$post['id'])->find();
            if (empty($find->id))
                return $this->fail('数据不存在');
            $find->save($post);
        }
        return $this->msg();
    }

    public function del(){
        $id = input('id');
        $find = model::where('id',$id)->find();
        if (empty($find->id))
            return $this->fail('数据不存在');
        $find->delete();
        return $this->msg();
    }
}